package com.wangxinenpu.springbootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxinenpu.springbootdemo.dao.mapper.FunctionMapper;
import com.wangxinenpu.springbootdemo.dataobject.dto.function.FunctionSaveVo;
import com.wangxinenpu.springbootdemo.dataobject.dto.function.FunctionTreeVo;
import com.wangxinenpu.springbootdemo.dataobject.model.Function;
import com.wangxinenpu.springbootdemo.service.FunctionService;
import com.wangxinenpu.springbootdemo.service.SystemService;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 功能点表 服务实现类
 * </p>
 *
 * @author =
 * @since 2024-06-14
 */
@Service
@Setter ( onMethod_ = @Resource )
public class FunctionServiceImpl extends ServiceImpl < FunctionMapper,Function > implements FunctionService {

	private SystemService sysService;

	@Override
	public R < List < FunctionTreeVo > > treeList() {
		// 全部节点
		List < Function > functionList = list();

		List < FunctionTreeVo > voArrayList = sysService.list().stream().map( item ->{
			FunctionTreeVo functionTreeVo = new FunctionTreeVo();
			functionTreeVo.setId( item.getId() );
			functionTreeVo.setFunctionCode( item.getSystemCode() );
			functionTreeVo.setFunctionName( item.getSystemName() );
			functionTreeVo.setChildLevel( getChildLevel( functionTreeVo , functionList ) );
			return functionTreeVo;
		} ).collect( Collectors.toList());

		return R.ok( voArrayList );
	}

	@Override
	public R < Boolean > delete( String id ) {
		// 节点下有数据不允许删除
		List < Function > functionList = list( new LambdaQueryWrapper < Function >().eq( Function::getParentId ,
				id ) );
		if ( ! CollectionUtils.isEmpty( functionList ) ) {
			return R.failed( "无法删除 , 请删除子数据" );
		}
		return R.ok( removeById( id ) );
	}

	@Override
	public R < Boolean > insert( FunctionSaveVo dto ) {
		Function functionFather = getById( dto.getId() );
		Function function = new Function()
				.setFunctionName( dto.getFunctionName() )
				.setFunctionCode( dto.getFunctionCode() )
				.setParentId( dto.getId() )
				.setFunType( dto.getFunctionType() )
				.setParentId( dto.getId() )
				.setRemark( dto.getRemark() )
				.setLevel( ObjectUtils.isEmpty( functionFather ) ? 1 : functionFather.getLevel() + 1 );
		return R.ok( save( function) );
	}

	@Override
	public R < Boolean > update( FunctionSaveVo dto ) {
		Function function = new Function();
		BeanUtils.copyProperties( dto , function );
		return R.ok( updateById( function ) );
	}

	@Override
	public R < Function > detail( String id ) {
		return R.ok( getById( id ) );

	}
	@Override
	public List < FunctionTreeVo > getChildLevel( FunctionTreeVo  functionFather , List < Function > functionList ) {
		List < FunctionTreeVo > treeVoList = new ArrayList <>();
		functionList.forEach( function -> {
			if ( function.getParentId().equals( functionFather.getId() ) ) {
				FunctionTreeVo childFunctionTreeVo = new FunctionTreeVo();
				BeanUtils.copyProperties( function , childFunctionTreeVo );
				childFunctionTreeVo.setChildLevel( getChildLevel( childFunctionTreeVo , functionList ) );
				treeVoList.add( childFunctionTreeVo );
			}
		} );
		return treeVoList;
	}

}
